import {_decorator, Component, instantiate, Node, Widget} from 'cc';
import {SettingManager} from "db://assets/script/modules/SettingManager";
import {ResourcesUtil} from "db://assets/script/utils/ResourcesUtil";
import {JoystickController} from "db://assets/script/core/JoystickController";
import {GameManager} from "db://assets/script/modules/GameManager";
import {ClickMoveController} from "db://assets/script/core/ClickMoveController";
import {SceneManager} from "db://assets/script/modules/SceneManager";

const {ccclass, property} = _decorator;

@ccclass('UiController')
export class UiController extends Component {

    @property({tooltip: '地图节点', type: Node})
    mapNode: Node

    @property({type: Node, tooltip: '加载'})
    loadingNode: Node = null

    joystickNode: Node = null

    async start() {
        SceneManager.setMapNode(this.mapNode)
        SceneManager.setLoadingNode(this.loadingNode)
        await SceneManager.initMap('test')
        await SceneManager.initMonster()
        await this.initControlType()
    }

    update() {
    }

    toggleControlType() {
        if (GameManager.isMoving) {
            console.log('busy!')
            return
        }
        SettingManager.setControlType(SettingManager.controlType === 'click' ? 'joystick' : 'click')
        const currentMapNode = SceneManager.mapNode.getChildByName(SceneManager.currentMapKey)
        if (SettingManager.controlType === 'joystick') {
            const clickJs = currentMapNode.getComponent(ClickMoveController)
            if (clickJs) {
                currentMapNode.removeComponent(clickJs)
            }
            this.initControlType()
        } else {
            if (this.joystickNode) {
                this.node.getChildByName("UI").removeChild(this.joystickNode)
            }
            currentMapNode.addComponent(ClickMoveController)
        }
    }

    async initControlType() {
        // console.log('SettingManager.controlType', SettingManager.controlType)

        if (SettingManager.controlType === 'joystick') {
            try {

                // 再实例化摇杆预制体
                const prefabKey: string = 'Joystick'
                const prefab = await ResourcesUtil.loadPrefab(`prefabs/${prefabKey}`)
                // 实例化预制体
                const joystickInstance = instantiate(prefab);
                if (!joystickInstance) {
                    console.log('摇杆实例化失败');
                    return;
                }
                joystickInstance.addComponent(JoystickController)
                const widget = joystickInstance.getComponent(Widget)
                widget.bottom = 0
                widget.left = 0
                widget.isAlignLeft = true
                widget.isAlignBottom = true
                this.joystickNode = joystickInstance
                this.node.getChildByName('UI').addChild(joystickInstance)
                widget.target = joystickInstance.parent
            } catch (e) {
                console.log('报错', e)
            }
        }
    }

}
